namer <- c('jons','labber','pteer','harro') 
for(i in 1:length(namer)){
  print(namer[i])
}

i <- 10
while (i>0){
  print('hello R')
  i <- i-1
}

if(is.character(i)){
  grade <- as.factor(i)
}
if(!is.factor(i)){
  grade <- as.factor(i)
  print('grade is factor')
}else{
  print('FALSE')
}
a <- 45
ifelse(a<45,print(a),print('not allowed'))

#用户自定义函数
mystats <- function(x,parameteric = TRUE, print = FALSE){
  if(parameteric){
    center <- mean(x)#均值
    spread <- sd(x)#标准差
  }else{
    center <- median(x)#中位数
    spread <- mad(x)#绝对中位差
  }
  if(print & parameteric){
    cat('mean=',center,'\n','sd=',spread,'\n')
  }else if(print & !parameteric){
    #cat...
    cat('median=',center,'\n','mad=',spread,'\n')
  }
  return(list(center=center,spread=spread))
}
set.seed(1234)
x <- rnorm(500)
y<- mystats(x,parameteric=FALSE,print=TRUE)

mydate <- function(type='long'){
  switch(type,
         long=format(Sys.time(),'%A %B %d %Y'),
         short=format(Sys.time(),'%m-%d-%y'),
         cat(type,'is not a recognized type\n'))
}
mydate('short') 

mtx <- matrix(c(1:20),nrow = 4, ncol = 5, byrow = FALSE)
mtx
t(roster)
#数据整合
options(digits=3)
with(mtcars,{
  adddata <- aggregate(mtcars,by=list(cyl,gear),FUN=mean,na.rm=TRUE) 
adddata

})
library(sqldf)
sqldf('SELECT cyl,gear,AVG(mpg) FROM mtcars GROUP BY cyl,gear')

#安装reshap2
install.packages('reshape2') 
library('reshape2')
id <- c(1,1,2,2)
time <- c(1,2,1,2)
x1 <- c(5,3,6,2)
x2 <- c(6,5,1,4)
mydata <- data.frame(id,time,x1,x2,stringsAsFactors = FALSE)
md <- melt(mydata,id=c('id','time'))



startdate <- as.Date('1992-11-15')
enddate <- Sys.Date()
enddate - startdate
format(startdate,'%Y-%m')



